package cn.zcib.film.controller;

import cn.zcib.film.anno.Record;
import cn.zcib.film.bean.Type;
import cn.zcib.film.bean.vo.TypeVO;
import cn.zcib.film.service.SubjectService;
import cn.zcib.film.service.TypeService;
import cn.zcib.film.util.DataGridViewResult;
import cn.zcib.film.util.SystemConstants;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/type")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @Resource
    private SubjectService subjectService;
    /**
     * 查询类型列表
     * @return
     */
    @RequestMapping("/list")
    public DataGridViewResult findSubs(TypeVO typeVO){
        //设置分页信息
        PageHelper.startPage(typeVO.getPage(),typeVO.getLimit());
        //调用查询电影方法

        List<Type> types = typeService.findTypes(typeVO);

        //创建分页对象
        PageInfo<Type> pageInfo = new PageInfo<>(types);

//        System.out.println("pageInfo = " + pageInfo);
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //添加类型
    @RequestMapping("/add")
    public String addType(Type type){
        HashMap<String, Object> map = new HashMap<>();

        if (typeService.addType(type)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "添加成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "添加失败");
        }
        return JSON.toJSONString(map);
    }

    //更新类型
    @RequestMapping("/update")
    public String updateSub(Type type){
        HashMap<String, Object> map = new HashMap<>();

        if (typeService.modType(type)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "修改成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "修改失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 检查这个类型是否存在电影
     * @param typeId
     * @return
     */
    @RequestMapping("/checkTypeHasSubject")
    public String checkTypeHasSubject(Integer typeId){
        HashMap<String, Object> map = new HashMap<>();

        if (subjectService.getSubCountByTypeId(typeId)>0){
            map.put(SystemConstants.EXIST, true);
            map.put(SystemConstants.MESSAGE, "该类型下存在电影信息，无法删除");
        }else {
            map.put(SystemConstants.EXIST, false);
        }
        return JSON.toJSONString(map);
    }

    /**
     * 检查这些类型是否存在电影
     * @param tIdsJSON  类型集合JSON类型
     * @return
     */
    @RequestMapping("/checkTypesHasSubject")
    public String checkTypesHasSubject(String tIdsJSON){
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> tIds=  JSONObject.parseArray(tIdsJSON,Integer.class);
        if (subjectService.getSubCountByTypeIds(tIds)>0){
            map.put(SystemConstants.EXIST, true);
            map.put(SystemConstants.MESSAGE, "选中的类型下存在电影信息，无法删除");
        }else {
            map.put(SystemConstants.EXIST, false);
        }
        return JSON.toJSONString(map);
    }

    //删除类型
    @RequestMapping("/deleteById")
    public String deleteById(Integer typeId){
        HashMap<String, Object> map = new HashMap<>();

        if (typeService.deleteById(typeId)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }
    //批量删除类型
    @RequestMapping("/groupDel")
    public String groupDelSub(String tIdsJSON){
        HashMap<String, Object> map = new HashMap<>();
        List<Integer> tIds=  JSONObject.parseArray(tIdsJSON,Integer.class);
        if (typeService.deleteByIds(tIds)>0){
            map.put(SystemConstants.SUCCESS, true);
            map.put(SystemConstants.MESSAGE, "删除成功");
        }else {
            map.put(SystemConstants.SUCCESS, false);
            map.put(SystemConstants.MESSAGE, "删除失败");
        }
        return JSON.toJSONString(map);
    }

    /**
     * 查询电影列表(下拉框)
     * @return
     */
    @RequestMapping("/findType")
    public String  findType(){
        return JSON.toJSONString(typeService.findTypes(null));
    }

    @RequestMapping(value = "/TopLove",produces = {"application/json;charset=UTF-8"})
    public String  TopLove(){
        List<Map<String, Integer>> mapList = typeService.getUserTopType();
        return JSON.toJSONString(mapList);
    }
}
